package com.smarthealthring.mm.ui.activity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.smarthealthring.mm.R;
import com.smarthealthring.mm.base.BaseActivity;
import com.smarthealthring.mm.base.IPresenter;
import com.smarthealthring.mm.utils.statusbar.StatusBarCompat;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * 计时和计里程页面
 */
public class CountTourActivity extends BaseActivity {
    @BindView(R.id.iv_left)
    ImageView mIvLeft;
    @BindView(R.id.iv_continue)
    ImageView mIvContinue;
    @BindView(R.id.iv_finsih)
    ImageView mIvFinish;
    @BindView(R.id.iv_pause)
    ImageView mIvPause;
    @BindView(R.id.ll_content)
    LinearLayout mLlContent;

    @BindView(R.id.ll1)
    LinearLayout mLl1;
    @BindView(R.id.ll2)
    LinearLayout mLl2;
    @BindView(R.id.tv_time1)
    TextView mTvTime1;
    @BindView(R.id.tv_time2)
    TextView mTvTime2;
    private Disposable disposable;
    private ObjectAnimator animator;
    private long preTime;

    @Override
    protected void initComToolBar() {

    }

    @Override
    protected int getLayout() {
        return R.layout.act_counttour;
    }

    @Override
    protected void initEventAndData(@Nullable Bundle savedInstanceState) {
        StatusBarCompat.setStatusBarColor(mContext, Color.parseColor("#2B5058"));
        startAlphaAnim();

        RxView.clicks(mIvLeft)
                .subscribe(o -> finish());

        //开始计时
        RxView.clicks(mIvContinue)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mLl1.setVisibility(View.INVISIBLE);
                        mLl2.setVisibility(View.VISIBLE);

                        startCountTime();
                        stopAlphaAnim();
                    }
                });

        RxView.longClicks(mIvFinish)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        finish();
                    }
                });

        //停止计时
        RxView.clicks(mIvPause)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mLl1.setVisibility(View.VISIBLE);
                        mLl2.setVisibility(View.INVISIBLE);

                        stopCountTime();
                        startAlphaAnim();
                    }
                });
    }

    private void startCountTime() {
        disposable = Flowable.interval(10, TimeUnit.MILLISECONDS)
                .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        long sec = (preTime + aLong * 10) / 1000;
                        long milSec = (preTime + aLong * 10) % 100;

                        long min = sec / 60;
                        long sec2 = sec % 60;

                        long hour = min / 60;
                        long min2 = min % 60;

                        long day = hour / 24;
                        long hour2 = hour % 24;

                        mTvTime1.setText(String.format("%1$02d:%2$02d:%3$02d", day, hour2, min2));
                        mTvTime2.setText(String.format("%1$02d:%2$02d", sec2, milSec));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }

    private void stopCountTime() {
        if (disposable != null) {
            disposable.dispose();
        }

        String str1 = mTvTime1.getText().toString().trim();
        String str2 = mTvTime2.getText().toString().trim();

        String[] split1 = str1.split(":");
        String[] split2 = str2.split(":");

        long preMilSec = Long.parseLong(split2[1]);
        long preSec2 = Long.parseLong(split2[0]);
        long preMin2 = Long.parseLong(split1[2]);
        long preHour2 = Long.parseLong(split1[1]);
        long preDay = Long.parseLong(split1[0]);

        preTime = preMilSec * 10 + preSec2 * 1000 + preMin2 * 1000 * 60 + preHour2 * 1000 * 60 * 60 +
                preDay * 1000 * 3600 * 24;

    }


    private void startAlphaAnim() {
        animator = ObjectAnimator.ofFloat(mLlContent, "alpha", 1, 0.0f);
        animator.setDuration(1000);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();
    }

    private void stopAlphaAnim() {
        animator.cancel();
        mLlContent.setAlpha(1);
    }

    @Override
    protected IPresenter createPresenter() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (animator != null) animator.cancel();
        if (disposable != null) disposable.dispose();
    }
}
